package com.mm.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.TreeMap;

public class DataokeUtils {

    /**
     * 淘口令解析出商品id
     *
     * @param content
     * @return
     */
    public static JSONObject getGoodsId(String content) {
        String url = "https://openapi.dataoke.com/api/tb-service/parse-taokouling";
        String appKey = "5f3bb88a9d465";
        String appSecret = "430b3bb77a24b25d3d7931be915d48a7";
        TreeMap<String, String> paraMap = new TreeMap<>();
        paraMap.put("version", "v1.0.0");
        paraMap.put("appKey", appKey);
        paraMap.put("content", content);
        String data = ApiClient.sendReq(url, appSecret, paraMap);
        System.out.println(data);
        JSONObject jsonObject = JSON.parseObject(data);
        JSONObject data1 = (JSONObject) jsonObject.get("data");
        return data1;
    }

    /**
     * 高佣转链
     *
     * @param goodsId
     * @return
     */
    public static String conventUrl(String goodsId) {
        String url = "https://openapi.dataoke.com/api/tb-service/get-privilege-link";
        String appKey = "5f3bb88a9d465";
        String appSecret = "430b3bb77a24b25d3d7931be915d48a7";
        TreeMap<String, String> paraMap = new TreeMap<>();
        paraMap.put("version", "v1.2.0");
        paraMap.put("appKey", appKey);
        paraMap.put("goodsId", goodsId);
        String data = ApiClient.sendReq(url, appSecret, paraMap);
        JSONObject jsonObject = JSON.parseObject(data);
        JSONObject data1 = (JSONObject) jsonObject.get("data");
        return data1.get("tpwd").toString();
    }

    /**
     * 商品详情
     *
     * @param goodsId
     * @return
     */
    public static JSONObject getGoodDetails(String goodsId) {
        String url = "https://openapi.dataoke.com/api/goods/get-goods-details";
        String appKey = "5f3bb88a9d465";
        String appSecret = "430b3bb77a24b25d3d7931be915d48a7";
        TreeMap<String, String> paraMap = new TreeMap<>();
        paraMap.put("version", "v1.2.3");
        paraMap.put("appKey", appKey);
        paraMap.put("goodsId", goodsId);
        String data = ApiClient.sendReq(url, appSecret, paraMap);
        JSONObject jsonObject = JSON.parseObject(data);
        JSONObject data1 = (JSONObject) jsonObject.get("data");
        return data1;
    }

    /**
     * 9.9包邮
     *
     * @param pageId  分页id：常规分页方式，请直接传入对应页码（比如：1,2,3……）
     * @param nineCid 9.9精选的类目id，分类id请求详情：-1-精选，1 -5.9元区，2 -9.9元区，3 -19.9元区
     * @return
     */
    public static JSONObject opGoodsList(String pageId, String nineCid) {
        String url = "https://openapi.dataoke.com/api/goods/nine/op-goods-list";
        String appKey = "5f3bb88a9d465";
        String appSecret = "430b3bb77a24b25d3d7931be915d48a7";
        TreeMap<String, String> paraMap = new TreeMap<>();
        paraMap.put("version", "v2.0.0");
        paraMap.put("appKey", appKey);
        paraMap.put("pageId", pageId);
        paraMap.put("pageSize", "100");
        paraMap.put("nineCid", nineCid);
        String data = ApiClient.sendReq(url, appSecret, paraMap);
        JSONObject jsonObject = JSON.parseObject(data);
        JSONObject data1 = (JSONObject) jsonObject.get("data");
        return data1;
    }

    /**
     * 商品列表
     *
     * @return
     */
    public static JSONObject getGoodList() {
        String url = "https://openapi.dataoke.com/api/goods/get-goods-list";
        String appKey = "5f3bb88a9d465";
        String appSecret = "430b3bb77a24b25d3d7931be915d48a7";
        TreeMap<String, String> paraMap = new TreeMap<>();
        paraMap.put("version", "v1.2.3");
        paraMap.put("appKey", appKey);
        paraMap.put("pageId", "1");
        paraMap.put("pageSize", "4");
        String data = ApiClient.sendReq(url, appSecret, paraMap);
        JSONObject jsonObject = JSON.parseObject(data);
        JSONObject data1 = (JSONObject) jsonObject.get("data");
        return data1;
    }

    /**
     * 每日爆品推荐
     *
     * @return
     */
    public static JSONArray explosiveGoodsList() {
        String url = "https://openapi.dataoke.com/api/goods/explosive-goods-list";
        String appKey = "5f3bb88a9d465";
        String appSecret = "430b3bb77a24b25d3d7931be915d48a7";
        TreeMap<String, String> paraMap = new TreeMap<>();
        paraMap.put("version", "v1.0.0");
        paraMap.put("appKey", appKey);
        paraMap.put("pageSize", "10");
        paraMap.put("pageId", "1");
        String data = ApiClient.sendReq(url, appSecret, paraMap);
        JSONObject jsonObject = JSON.parseObject(data);
        JSONArray list = jsonObject.getJSONObject("data").getJSONArray("list");
        return list;
    }

    public static void main(String[] args) {
     /*   String s = conventUrl("613200652268");
        JSONObject goodDetails = getGoodDetails("613200652268");
        String title = goodDetails.get("title").toString();
        String mainPic = goodDetails.get("mainPic").toString();
        //商品原价
        String originalPrice = goodDetails.get("originalPrice").toString();
        //券后价
        String actualPrice = goodDetails.get("actualPrice").toString();
        //优惠券额度
        String couponPrice = goodDetails.get("couponPrice").toString();*/
        for (int i = 0; i < explosiveGoodsList().size(); i++) {
            System.out.println(explosiveGoodsList().getJSONObject(i).get("mainPic"));

        }
        System.out.println(explosiveGoodsList());
//        System.out.println(explosiveGoodsList().get("mainPic"));
    }

    /**
     * 搜索
     *
     * @return
     */
    public static JSONObject search(String pageId, String keyWords, String sort) {
        String url = "https://openapi.dataoke.com/api/goods/get-dtk-search-goods";
        String appKey = "5f3bb88a9d465";
        String appSecret = "430b3bb77a24b25d3d7931be915d48a7";
        TreeMap<String, String> paraMap = new TreeMap<>();
        paraMap.put("version", "v2.1.2");
        paraMap.put("appKey", appKey);
        paraMap.put("pageId", pageId);
        paraMap.put("pageSize", "50");
        paraMap.put("sort", sort);
        paraMap.put("keyWords", keyWords);
        String data = ApiClient.sendReq(url, appSecret, paraMap);
        JSONObject jsonObject = JSON.parseObject(data);
        JSONObject data1 = (JSONObject) jsonObject.get("data");
        return data1;
    }

}
